<?php

namespace Datacore\dao;

use Datacore\Interfaces\IBasicIinterface;

class BasicDao implements IBasicIinterface
{
    protected $modelName = null;
    private $tableName = null;
    private $h = "localhost";
    private $user = "root";
    private $pass = "root123456";
    private $db = "admin";
    protected $dbPrefix = null;
    
    //获得连接
    public function getConnection()
    {
        $conn = mysqli_connect($this->h, $this->user, $this->pass, $this->db);
        return $conn;
    }
    
    //初始化
    public function init()
    {
        //根据model的名字得到表的名字
        //$this->tableName = strtolower(substr($this->modelName,0,2))."_".strtolower(substr($this->modelName,2));
        $this->tableName = $this->dbPrefix . substr($this->modelName, 0, strrpos($this->modelName, "M"));
    }
    
    //获得一个表的列名
    public function getColumn($tableName)
    {
        $sql = "show columns from " . $tableName;
        $conn = $this->getConnection();
        $columns = array();
        if ($conn != null) {
            $rtn = mysqli_query($conn, $sql);
            while ($rtn !== false && ($row = mysqli_fetch_array($rtn)) != null) {
                $columns[] = $row[0];
            }
            mysqli_close($conn);
        }
        return $columns;
    }
    
    public function count($where)
    {
        $sql = "select count(*) as counts from " . $this->tableName . " where " . $where." limit 1";
        $conn = $this->getConnection();
        $arr = array();
        if ($conn != null) {
            $rtn = mysqli_query($conn, $sql);
            while ($rtn !== false && ($row = mysqli_fetch_array($rtn)) != null) {
                $counts = $row['counts'];
            }
           
            mysqli_close($conn);
        }
        return $counts;
    }
    
    /**
     * @param $where
     * @return array
     */
    public function findByWhere($where)
    {
        //获得数据表的列名
        $columns = $this->getColumn($this->tableName);
        //拼接sql语句
        $sql = "select * from " . $this->tableName . " where " . $where;
        $conn = $this->getConnection();
        $arr = array();
        if ($conn != null) {
            $rtn = mysqli_query($conn, $sql);
            while ($rtn !== false && ($row = mysqli_fetch_array($rtn)) != null) {
                $index = -1;
                $class='\Datacore\model\\' . $this->modelName;
                $obj = new $class();
                foreach ($columns as $column) {
                    $obj->{"set" . ucfirst($column)}($row[++$index]);
                }
                $arr[] = $obj;
            }
            mysqli_close($conn);
        }
        return $arr;
    }
    
    /**
     * 分页查询；支持排序
     * @param $where
     * @param $order
     * @param null $start
     * @param null $limit
     * @return array
     */
    public function findWhereOrderBy($where, $order,$start,$limit)
    {
        
        //获得数据表的列名
        $columns = $this->getColumn($this->tableName);
        //拼接sql语句
        $sql = "select * from " . $this->tableName . " where " . $where . " order by " . $order." limit " . $start . "," . $limit;
        $conn = $this->getConnection();
        $arr = array();
        if ($conn != null) {
            $rtn = mysqli_query($conn, $sql);
            while ($rtn !== false && ($row = mysqli_fetch_array($rtn)) != null) {
                $index = -1;
                $class='\Datacore\model\\' . $this->modelName;
                $obj = new $class();
                foreach ($columns as $column) {
                    $obj->{"set" . ucfirst($column)}($row[++$index]);
                }
                $arr[] = $obj;
            }
            mysqli_close($conn);
        }
        return $arr;
    }
    
    /**
     * 添加
     * @param $obj
     * @return bool
     */
    public function save($obj)
    {
        
        $columns = $this->getColumn($this->tableName);
        $conn = $this->getConnection();
        $tag = false;
        if ($conn != null) {
            $sql = "insert into " . $this->tableName . "(";
            foreach ($columns as $column) {
                $sql .= $column . ",";
            }
            $sql = substr($sql, 0, strlen($sql) - 1) . ") values(";
            foreach ($columns as $column) {
                $value = $obj->{"get" . ucfirst($column)}();
                //判断$value的类型
                if ($value == null) {
                    //$sql .= "null,";
                    unset($column);
                    continue;
                } else if (preg_match("/^[0-9]*$/", $value)) {
                    //是数字
                    $sql .= $value . ",";
                } else {
                    $sql .= "'" . $value . "',";
                }
            }
            $sql = substr($sql, 0, strlen($sql) - 1);
            $sql .= ")";
            //执行sql语句
            mysqli_query($conn, $sql);
            $tag = true;
            mysqli_close($conn);
        }
        return $tag;
    }
    
    /**
     * @param $obj
     * @return bool
     * 删除
     */
    public function delete($obj)
    {
        
        $conn = $this->getConnection();
        $tag = false;
        if ($conn != null) {
            $sql = "delete from " . $this->tableName . " where ";
            $columns = $this->getColumn($this->tableName);
            $value = $obj->{"get" . ucfirst($columns[0])}();
            if ($value != null) {
                //是数字
                if (preg_match("/^[0-9]*$/", $value)) {
                    $sql .= $columns[0] . "=" . $value;
                } else {
                    $sql .= $columns[0] . "='" . $value . "'";
                }
                //执行
                mysqli_query($conn, $sql);
                $tag = true;
            }
            mysqli_close($conn);
        }
        return $tag;
        
    }
    
    /**
     * @param $obj
     * @return bool
     * 更新
     */
    public function update($obj)
    {
        
        $conn = $this->getConnection();
        $columns = $this->getColumn($this->tableName);
        $tag = false;
        if ($conn != null) {
            $sql = "update " . $this->tableName . " set ";
            for ($i = 1; $i < count($columns); $i++) {
                $column = $columns[$i];
                $value = $obj->{"get" . ucfirst($columns[$i])}();
                if ($value == null) {
                    //$sql .= $column . "=null,";
                    unset($column);
                    continue;
                } else if (preg_match("/^[0-9]*$/", $value)) {
                    $sql .= $column . "=" . $value . ",";
                } else {
                    $sql .= $column . "='" . $value . "',";
                }
            }
            $sql = substr($sql, 0, strlen($sql) - 1);
            $sql .= " where ";
            $tempColumn = $columns[0];
            $tempValue = $obj->{"get" . ucfirst($columns[0])}();
            if (preg_match("/^[0-9]*$/", $tempValue)) {
                $sql .= $tempColumn . "=" . $tempValue;
            } else {
                $sql .= $tempColumn . "='" . $tempValue . "'";
            }
            //执行操作
            $res = mysqli_query($conn, $sql);
            if($res){
                $tag = true;
            }
            mysqli_close($conn);
        }
        return $tag;
    }
    
    /**
     * @param $sql
     * @param $columns
     * @param $where
     * @param null $start
     * @param null $limit
     * @return array
     * 按查询列执行sql生成实体
     */
    public function Querys($sql,$columns,$where,$start = null, $limit = null)
    {
        if ($start != null && $limit != null) {
            $sql .= "limit " . $start . "," . $limit;
        }
        $conn = $this->getConnection();
        $arr = array();
        if ($conn != null) {
            $rtn = mysqli_query($conn, $sql);
            while ($rtn !== false && ($row = mysqli_fetch_array($rtn)) != null) {
                $index = -1;
                $class='\Datacore\model\\' . $this->modelName;
                $obj = new $class();
                foreach ($columns as $column) {
                    $obj->{"set" . ucfirst($column)}($row[++$index]);
                }
                $arr[] = $obj;
            }
            mysqli_close($conn);
        }
        return $arr;
        
    }
    
    public function getOne($where)
    {
        //获得数据表的列名
        $columns = $this->getColumn($this->tableName);
        //拼接sql语句
        $sql = "select * from " . $this->tableName . " where " . $where." limit 1";
        $conn = $this->getConnection();
        $arr = array();
        if ($conn != null) {
            $rtn = mysqli_query($conn, $sql);
            while ($rtn !== false && ($row = mysqli_fetch_array($rtn)) != null) {
                $index = -1;
                $class='\Datacore\model\\' . $this->modelName;
                $obj = new $class();
                foreach ($columns as $column) {
                    $obj->{"set" . ucfirst($column)}($row[++$index]);
                }
                $arr[] = $obj;
            }
            mysqli_close($conn);
        }
        return $arr[0];
    }
    
    public function getOneById($idString,$id){
        //获得数据表的列名
        $columns = $this->getColumn($this->tableName);
        //拼接sql语句
        $sql = "select * from " . $this->tableName . " where " .$idString."=".$id." limit 1";
        $conn = $this->getConnection();
        $arr = array();
        if ($conn != null) {
            $rtn = mysqli_query($conn, $sql);
            while ($rtn !== false && ($row = mysqli_fetch_array($rtn)) != null) {
                $index = -1;
                $class='\Datacore\model\\' . $this->modelName;
                $obj = new $class();
                foreach ($columns as $column) {
                    $obj->{"set" . ucfirst($column)}($row[++$index]);
                }
                $arr[] = $obj;
            }
            mysqli_close($conn);
        }
        return $arr[0];
    }
}